home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / QuickDraw / ZoomRecter / main.c next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  4.3 KB  |  202 lines  |  [TEXT/KAHL]

  1. /*
  2.     Zoom-Rect-er
  3.     DTS Code Snippet
  4.     
  5.     1/6/92    Steve Falkenburg
  6.     
  7.     This snippet shows how to do "Finder" style zooming between two rectangles.
  8.     The boolean flag "kZoomLarger" controls the proportional direction of the zooming.
  9.     
  10.     To get the two rectangles, you drag them out rubberbanded, and the zoom occurs between
  11.     them.  To quit, click the close box.
  12.     
  13.     If you want to do zooms between windows, open up a port with the dimensions of the desktop
  14.     (from GetGrayRgn()).
  15.     
  16.     DON'T use this as a sample of how to do rubberband drawing!!!  It's sort of hacked
  17.     together bypassing the event mechanism and just using Button().
  18. */
  19.  
  20.     
  21.  
  22. #define    kNumSteps        14
  23. #define    kRectsVisible    4
  24. #define    kZoomRatio        .7
  25. #define    kDelayTicks        1
  26.  
  27. #define    kZoomLarger        true        // change this to zoom "inward"
  28.  
  29.  
  30. void InitStuff(void);
  31. void ZoomRect(Boolean zoomOut,Rect *smallRect, Rect *bigRect);
  32. void CalcRect(Rect *theRect,Rect *smallRect,Rect *bigRect,double stepValue);
  33. Boolean GetRects(Rect *zoomFrom,Rect *zoomTo);
  34. void FixRect(Rect *theRect,Rect *rightRect);
  35.  
  36. Boolean gDone;
  37.  
  38. void main(void)
  39. {
  40.     WindowPtr window;
  41.     Rect bounds = {44,12,330,500},zoomFrom,zoomTo;
  42.         
  43.     InitStuff();
  44.     window = NewWindow(nil,&bounds,"\pDrag Two Rects to Zoom",true,documentProc,(WindowPtr)-1L,true,0);
  45.     SetPort(window);
  46.     
  47.     do {
  48.         if (GetRects(&zoomFrom,&zoomTo))
  49.             ZoomRect(kZoomLarger,&zoomFrom,&zoomTo);
  50.         EraseRect(&window->portRect);
  51.     }
  52.     while (!gDone);
  53.     
  54.     FlushEvents(everyEvent,0);
  55. }
  56.  
  57.     
  58. void InitStuff(void)
  59. {
  60.     InitGraf(&qd.thePort);
  61.     InitFonts();
  62.     InitWindows();
  63.     InitMenus();
  64.     TEInit();
  65.     InitDialogs(nil);
  66.     InitCursor();
  67.     FlushEvents(everyEvent,0);
  68. }
  69.  
  70.  
  71. void ZoomRect(Boolean zoomLarger,Rect *smallRect, Rect *bigRect)
  72. {
  73.     double firstStep,stepValue,trailer,zoomRatio;
  74.     short i,step;
  75.     Rect curRect;
  76.     unsigned long ticks;
  77.     
  78.     PenPat(qd.gray);
  79.     PenMode(patXor);
  80.     
  81.     
  82.     firstStep=kZoomRatio;
  83.     for (i=0; i<kNumSteps; i++) {
  84.         firstStep *= kZoomRatio;
  85.     }
  86.  
  87.     if (!zoomLarger) {
  88.         zoomRatio = 1.0/kZoomRatio;
  89.         firstStep = 1.0-firstStep;
  90.     }
  91.     else
  92.         zoomRatio = kZoomRatio;
  93.         
  94.     trailer = firstStep;
  95.     stepValue = firstStep;
  96.     for (step=0; step<(kNumSteps+kRectsVisible); step++) {
  97.     
  98.         // draw new frame
  99.         
  100.         if (step<kNumSteps) {
  101.             stepValue /= zoomRatio;
  102.             CalcRect(&curRect,smallRect,bigRect,stepValue);
  103.             FrameRect(&curRect);
  104.         }
  105.         
  106.         // erase old frame
  107.         
  108.         if (step>=kRectsVisible) {
  109.             trailer /= zoomRatio;
  110.             CalcRect(&curRect,smallRect,bigRect,trailer);
  111.             FrameRect(&curRect);
  112.         }
  113.  
  114.         Delay(kDelayTicks,&ticks);
  115.     }
  116.  
  117.     PenNormal();
  118. }
  119.  
  120.  
  121. void CalcRect(Rect *theRect,Rect *smallRect,Rect *bigRect,double stepValue)
  122. {
  123.     theRect->left = smallRect->left + (short)((double)(bigRect->left-smallRect->left)*stepValue);
  124.     theRect->top = smallRect->top + (short)((double)(bigRect->top-smallRect->top)*stepValue);
  125.     theRect->right = smallRect->right + (short)((double)(bigRect->right-smallRect->right)*stepValue);
  126.     theRect->bottom = smallRect->bottom + (short)((double)(bigRect->bottom-smallRect->bottom)*stepValue);
  127. }
  128.  
  129.  
  130. Boolean GetRects(Rect *zoomFrom,Rect *zoomTo)
  131. {
  132.     short numRects = 0;
  133.     EventRecord ev;
  134.     Rect theRect,drawRect;
  135.     Point firstPt,curPt,oldPt,globalPt;
  136.     KeyMap theKeys;
  137.     WindowPtr window;
  138.     
  139.     PenMode(patXor);
  140.     
  141.     do {
  142.         while (!Button());
  143.         
  144.         GetMouse(&globalPt);
  145.         LocalToGlobal(&globalPt);
  146.         if ((FindWindow(globalPt,&window)==inGoAway) && window==FrontWindow()) {
  147.             gDone = true;
  148.             return false;
  149.         }
  150.         
  151.         GetMouse(&firstPt);
  152.         oldPt = firstPt;
  153.         SetRect(&theRect,firstPt.h,firstPt.v,firstPt.h,firstPt.v);
  154.         while (Button()) {
  155.             GetMouse(&curPt);
  156.             if (!EqualPt(curPt,oldPt)) {
  157.                 FixRect(&theRect,&drawRect);
  158.                 FrameRect(&drawRect);
  159.                 oldPt = curPt;
  160.                 theRect.right = curPt.h;
  161.                 theRect.bottom = curPt.v;
  162.                 FixRect(&theRect,&drawRect);
  163.                 FrameRect(&drawRect);
  164.             }
  165.         }
  166.         
  167.         FixRect(&theRect,&drawRect);
  168.         if (numRects==0)
  169.             *zoomFrom = drawRect;
  170.         else
  171.             *zoomTo = drawRect;
  172.             
  173.         numRects++;
  174.  
  175.     } while (numRects<2);
  176.     
  177.     PenNormal();
  178. }
  179.  
  180.  
  181. void FixRect(Rect *theRect,Rect *rightRect)
  182. {
  183.     if (theRect->right > theRect->left) {
  184.         rightRect->right = theRect->right;
  185.         rightRect->left = theRect->left;
  186.     }
  187.     else {
  188.         rightRect->right = theRect->left;
  189.         rightRect->left = theRect->right;
  190.     }
  191.     
  192.     if (theRect->bottom > theRect->top) {
  193.         rightRect->bottom = theRect->bottom;
  194.         rightRect->top = theRect->top;
  195.     }
  196.     else {
  197.         rightRect->bottom = theRect->top;
  198.         rightRect->top = theRect->bottom;
  199.     }
  200.     
  201. }
  202.